package com.example.mymap_beta;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{

	private List<OverlayItem> list;
	/** GeoPoints representing this routePoints. **/
    private List<GeoPoint> routePoints;
    /** Colour to paint routePoints. **/
    private int colour;
    /** Alpha setting for route overlay. **/
    private static int ALPHA = 120;
    /** Stroke width. **/
    private static float STROKE = 4.5f;
    /** Route path. **/
    private Path path;
    /** Point to draw with. **/
    private Point p;
    /** Paint for path. **/
    private Paint paint;
    
    
	
	public MyItemizedOverlay(Drawable arg0, Route route, int defaultColor) {
		super(boundCenter(arg0));
		list = new ArrayList<OverlayItem>();
		
		routePoints = route.getPoints();
        colour = defaultColor;
        path = new Path();
        p = new Point();
        paint = new Paint();
	}

	@Override
	protected OverlayItem createItem(int arg0) {
		return list.get(arg0);
	}

	@Override
	public int size() {
		return list.size();
	}

	public void addItem(OverlayItem item) {
		list.add(item);
		populate();
	}
	
	@Override
	public void draw(Canvas c, MapView mv, boolean shadow) {
		
		super.draw(c, mv, shadow);

        paint.setColor(colour);
        paint.setAlpha(ALPHA);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(STROKE);
        paint.setStyle(Paint.Style.STROKE);

        redrawPath(mv);
        c.drawPath(path, paint);

	}
	
    /**
     * Clear the route overlay.
     */
    public final void clear() {
            routePoints.clear();
    }

    /**
     * Recalculate the path accounting for changes to
     * the projection and routePoints.
     * @param mv MapView the path is drawn to.
     */
    private void redrawPath(final MapView mv) {
        final Projection prj = mv.getProjection();
        path.rewind();
        final Iterator<GeoPoint> it = routePoints.iterator();
        prj.toPixels(it.next(), p);
        path.moveTo(p.x, p.y);
        while (it.hasNext()) {
                prj.toPixels(it.next(), p);
                path.lineTo(p.x, p.y);
        }
        path.setLastPoint(p.x, p.y);
    }
}
